package com.zhiteng.service.impl;

import java.text.SimpleDateFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiteng.entity.ActualNode;
import com.zhiteng.entity.AdjustBill;
import com.zhiteng.mapper.ActualNodeMapper;
import com.zhiteng.service.ActualNodeService;
import com.zhiteng.utils.common.DateUtil;

@Service
public class ActualNodeServiceImpl implements ActualNodeService {

	@Autowired
	private ActualNodeMapper actualNodeMapper;

	@Override
	public void saveActualNode(AdjustBill bill) {

		if (bill.getType2() != 2) {
			ActualNode record = new ActualNode();
			record.setSchoolId(bill.getSchool_id());
			record.setTeacherId(bill.getPetitioner_id());
			record.setTeacherName(bill.getPetitioner_name());

			record.setCourseName(bill.getPetitioner_subject());
			record.setAdjust_date(bill.getAdjust_date());
			record.setWeek(bill.getWeek());

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

			int weekly_times = DateUtil.getWeek(sdf.format(bill.getAdjust_date()));
			record.setWeekly_times(weekly_times);
			record.setClassName(bill.getOld_class());
			record.setCourseNum(bill.getOld_node());
			record.setBill_id(bill.getId());
			record.setType2(bill.getType2());
			record.setGradeName(bill.getGrade_name());
			actualNodeMapper.insert(record);

			ActualNode record2 = new ActualNode();
			record2.setSchoolId(bill.getSchool_id());
			record2.setTeacherId(bill.getTeacher_id());
			record2.setTeacherName(bill.getTeacher_name());

			record2.setCourseName(bill.getOld_subject());
			record2.setAdjust_date(bill.getPetitioner_date());
			record2.setWeek(bill.getPetitioner_week());

//			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

			int weekly_times2 = DateUtil.getWeek(sdf.format(bill.getPetitioner_date()));
			record2.setWeekly_times(weekly_times2);
			record2.setClassName(bill.getPetitioner_class());
			record2.setCourseNum(bill.getCourse_num());
			record2.setBill_id(bill.getId());
			record2.setType2(bill.getType2());
			record2.setGradeName(bill.getPetitioner_gradename());
			actualNodeMapper.insert(record2);
		} else {
			ActualNode record = new ActualNode();
			record.setSchoolId(bill.getSchool_id());
			record.setTeacherId(bill.getPetitioner_id());
			record.setTeacherName(bill.getPetitioner_name());

			record.setCourseName(bill.getPetitioner_subject());
			record.setAdjust_date(bill.getAdjust_date());
			record.setWeek(bill.getWeek());

			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

			int weekly_times = DateUtil.getWeek(sdf.format(bill.getAdjust_date()));
			record.setWeekly_times(weekly_times);
			record.setClassName(bill.getOld_class());
			record.setCourseNum(bill.getOld_node());
			record.setBill_id(bill.getId());
			record.setType2(bill.getType2());
			record.setGradeName(bill.getGrade_name());
			actualNodeMapper.insert(record);
		}
	}

	@Override
	public List<ActualNode> queryActualNodeByWeeks(String school_id, Integer weeksInYear) {
		ActualNode record = new ActualNode();
		record.setSchoolId(school_id);
		record.setWeekly_times(weeksInYear);
		return actualNodeMapper.select(record);
	}

	@Override
	public List<ActualNode> queryActualNodeBySchoolIdAndTeacherIdAndPetitionerDate(AdjustBill bill) {

		ActualNode record2 = new ActualNode();
		record2.setSchoolId(bill.getSchool_id());

		record2.setAdjust_date(bill.getPetitioner_date());
		record2.setWeek(bill.getPetitioner_week());

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		int weekly_times2 = DateUtil.getWeek(sdf.format(bill.getPetitioner_date()));
		record2.setWeekly_times(weekly_times2);
		record2.setClassName(bill.getPetitioner_class());
		record2.setCourseNum(bill.getCourse_num());
		return actualNodeMapper.select(record2);
	}
}
